package org.mspring.platform.config.dao;

import java.io.IOException;
import java.io.InputStream;

import org.apache.commons.io.IOUtils;
import org.mspring.platform.config.entity.Config;
import org.mspring.platform.dao.hibernate.GenericDao;
import org.springframework.stereotype.Repository;

/**
 *
 * @author Gao Youbo
 * @since 2014年4月13日
 */
@Repository
public class ConfigDaoImpl extends GenericDao<Config, Long> implements ConfigDao {

    @Override
    public Config findByName(String key) {
        return super.uniqueResult("select config from Config config where config.name = ?", key);
    }

    @Override
    public void deleteByName(String key) {
        super.executeUpdate("delete from Config config where config.name = ?", key);
    }

    @Override
    public void initTable() throws IOException {
        try (InputStream is = getClass().getResourceAsStream("/t_config.sql")) {
            String sql = IOUtils.toString(is, "UTF-8");
            getJdbcTemplate().execute(sql);
        }
    }

}